window.main =function (){
    var sceneId;
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    sceneId=getUrlParam("sceneId")
    $("#left-scene").click(function(){
        $("#layer").css("display","block");
        $("#layer_iframe").css("margin-left","312px");
        $("#layer_iframe").css("padding","0px");
        $("#left-scene").addClass("active");
        $(".layer_off").css("display","none");
        $(".layer_on").css("display","block");
        $("#layerConfig").css("display", "none");
        $("#left-layer").removeClass("active");
        $(".layerConfig_on").css("display","none");
        $(".layerConfig_off").css("display","block");
        $("#sceneManage").addClass("active");
    });


    $("#logo_close").click(function () {
        $("#layer").css("display", "none");
        $("#layer_iframe").css("margin-left","56px");
        $("#left-scene").removeClass("active");
        $(".layer_on").css("display","none");
        $(".layer_off").css("display","block");
    });

    $("#layerBack").click(function () {
        // self.location=document.referrer;//去另一个页面刷新
        // window.location.replace(document.referrer);//返回上一页刷新
        // window.location.href=window.close();  //关闭子窗口
        window.close();

        //  window.location.href="http://localhost:9090";//返回上一页刷新
        // window.open(window.url);
        // window.history.go(-1);
        // window.location.go(-1);

    });
    $("#left-layer").click(function(){
        $("#layerConfig").css("display","block");
        $("#layer_iframe").css("margin-left","312px");
        $("#layer_iframe").css("padding","0px");
        $("#left-layer").addClass("active");
        $(".layerConfig_off").css("display","none");
        $(".layerConfig_on").css("display","block");
        $("#layer").css("display", "none");
        $("#left-scene").removeClass("active");
        $(".layer_on").css("display","none");
        $(".layer_off").css("display","block");
        $("#sceneManage").addClass("active");
        $("#layerConfig").css("height","calc(100% - 56px)");
    });
    $("#layer_close").click(function () {
        $("#layerConfig").css("display", "none");
        $("#layer_iframe").css("margin-left","56px");
        $("#left-layer").removeClass("active");
        $(".layerConfig_on").css("display","none");
        $(".layerConfig_off").css("display","block");
    });

    $("#layer_add").hover(function(){
        $("#layer_create").css("display","block");
    }, function(){
        setTimeout(function () {
            $("#layer_create").css("display","none");
        },3000);
    });

    $("#sceneManage").click(function(){
        $("#leftBar").css("display", "block");
        $("#layer_iframe").css("margin-left","56px");
        $("#sceneManage").addClass("active");
        $("#sceneNavigate").removeClass("active");
        var childWindow = $("#layer_iframe")[0].contentWindow;//调用iframe页面
        childWindow.close_sceneNavigate();
    });
    $("#sceneNavigate").click(function(){
        $("#leftBar").css("display", "none");
        $("#layer").css("display", "none");
        $("#layerConfig").css("display", "none");
        $("#layer_iframe").css("margin-left","0px");
        $("#sceneNavigate").addClass("active");
        $("#sceneManage").removeClass("active");
        var childWindow = $("#layer_iframe")[0].contentWindow;//调用iframe页面
        childWindow.sceneNavigate();
    });
//初始场景封面
    function dataURLtoFile(image, filename = 'file') {
        let arr = image.split(',');
        let mime = arr[0].match(/:(.*?);/)[1];
        let suffix = mime.split('/')[1];
        let bstr = atob(arr[1]);
        let n = bstr.length;
        let u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], `${filename}.${suffix}`, {
            type: mime
        })
    }

    $("#setCover").click(function(){
        var childWindow = $("#layer_iframe")[0].contentWindow;//调用iframe页面
        var viewer=childWindow.viewer;
        var image;//创建img对象
        viewer.render();//重新渲染界面
        image = viewer.scene.canvas.toDataURL("image/jpeg",1.75);//base64图片数据
        var base64Img = image; // base64编码的图片
        var imgFile = dataURLtoFile(base64Img);
        var fromObj=new FormData();
        //获取图片对象
        // var files=$("#setCover")[0].files;
        //将图片对象 添加进待上传参数中
        fromObj.append('file',imgFile);
        //照片传到后台
        $.ajax({
            url: "/one/file/upload",
            type: "POST",
            processData: false,
            contentType: false,
            data: fromObj,
            success: function (res) {
                var getPhoto;
                getPhoto=res.data;
                var photoUrl= "/one/file/preview/url"+'?url='+getPhoto;
                $("#coverScene").attr("src", photoUrl);
                $.ajax({
                    url: "/one/scene",
                    type: "PUT",
                    dataType: "json",
                    contentType: "application/json; charset=UTF-8",
                    data:JSON.stringify({
                        coverUrl:getPhoto,
                        id:sceneId,
                    }),
                    success: function (result) {
                        if (result.errorCode === '401') {
                            loginStatusIntercept(result.msg)
                            return
                        }
                        if(result.errorCode==0){
                            layer.msg("设置成功", {icon: 6});
                        }else{
                            layer.msg("设置失败", {icon: 5});
                        }
                    }
                });
            }
        });
    });
//canvas保存图片到本地
    /*    (function(t){
            var dlLink = t || document.createElement("a");
            if(!t){
                dlLink.id='dlLink';
                dlLink.download = '文件名';
                document.body.appendChild(dlLink);
            }
            dlLink.href = image;
            dlLink.click();
        })(document.querySelector("#dlLink"));*/


    $("#setAngle").click(function(){
        childWindow = $("#layer_iframe")[0].contentWindow;//调用iframe页面
        var viewer=childWindow.viewer;


        var getDefaultExtent=getDefaultExtent();
        var getDefaultAngle=JSON.stringify(getDefaultExtent);

        var getThisExtent=getThisExtent();
        var getAngle=JSON.stringify(getThisExtent);

        // var bookmarkGet=JSON.parse(getThisExtent);
        /*var showExtent=showExtent(getDefaultExtent);
        console.log(showExtent);*/

        function showExtent(cfg) {
            console.log('书签定位：' + JSON.stringify(cfg));
            viewer.mars.centerAt(cfg, { isWgs84: true });
        }
        function getDefaultExtent() {
            return viewer.gisdata.config.center;
        }
        function getThisExtent() {
            var bookmark = childWindow.mars3d.point.getCameraView(viewer, true);
            return bookmark;
        }

        var childWindow = $("#layer_iframe")[0].contentWindow;//调用iframe页面
        var viewer=childWindow.viewer;
        var image;//创建img对象
        viewer.render();//重新渲染界面
        image = viewer.scene.canvas.toDataURL("image/jpeg",0.075);//base64图片数据
        var base64Img = image; // base64编码的图片
        var imgFile = dataURLtoFile(base64Img);
        var fromObj=new FormData();
        //将图片对象 添加进待上传参数中
        fromObj.append('file',imgFile);
        //照片传到后台
        $.ajax({
            url: "/one/file/upload",
            type: "POST",
            processData: false,
            contentType: false,
            data: fromObj,
            success: function (res) {
                var getPhoto=res.data;
                var photoUrl= "/one/file/preview/url"+'?url='+getPhoto;
                $("#angleScene").attr("src", photoUrl);
                //修改场景视角和图片
                $.ajax({
                    url: "/one/scene",
                    type: "PUT",
                    dataType: "json",
                    contentType: "application/json; charset=UTF-8",
                    data:JSON.stringify({
                        angle:getAngle,
                        angleUrl:getPhoto,
                        id:sceneId,
                    }),
                    success: function (result) {
                        if (result.errorCode === '401') {
                            loginStatusIntercept(result.msg)
                            return
                        }
                        if(result.errorCode==0){
                            layer.msg("设置成功", {icon: 6});
                            var bookmarkGet=JSON.parse(getAngle);
                            showExtent(bookmarkGet);
                        }else{
                            layer.msg("设置失败", {icon: 5});
                        }
                    }
                });

            }
        });
    });

}
